//
//  CYInviteViewController.m
//  Rabbit
//
//  Created by 阿帕 on 2018/11/3.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "HuoYuanViewController.h"
#import "HuoYuanDetailViewController.h"
#import "HuoYuanListViewController.h"
#import "AddOftenRunRouteViewController.h"
#import "SearchHuoYuanViewController.h"
#import "ScanViewController.h"
#import "LBXScanViewStyle.h"

#import "SGPagingView.h"

#import "SupplyCell.h"
#import "OftenRunCell.h"

#import "MainClassModel.h"
#import "SupplyModel.h"
#import "CYCityChooseView.h"
#import "DriverAuthVC.h"

@interface HuoYuanViewController ()<UITableViewDelegate , UITableViewDataSource, UINavigationControllerDelegate, SGPageTitleViewDelegate, CYChooseRegionDelegate, HuoYuanSearchConditionDelegate>
{
    NSString *_chufaAddress;
    NSString *_chufaCode;
    NSString *_shouhuoAddress;
    NSString *_shouhuoCode;
    
    BOOL _isSegmentedLeft;
    BOOL _isChufaFlag;
}
@property (strong , nonatomic)UITableView *tableView;
@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,strong)UIView *barrierView;
@property (nonatomic, strong) UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *oftenRunArray;
@property (nonatomic, strong) NSMutableArray *provinceSouce;
@property (nonatomic, strong) NSMutableArray *citySouce;
@property (nonatomic, strong) NSMutableArray *countySouce;
@property (strong , nonatomic)UIButton *confirmButton;
@property (strong , nonatomic)NSDictionary *searchDic;

@end

@implementation HuoYuanViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!TOKEN_ISEMPTY) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            for (int i = 1; i<=3; i++) {
                [UIView animateWithDuration:0.5f animations:^{
                    UIView *view = [self.tabBarController.view viewWithTag:CJAlertDueTag+i];
                    view.alpha = 1;
                } completion:nil];
            }
        });
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    for (int i = 1; i<=3; i++) {
        UIView *view = [self.tabBarController.view viewWithTag:CJAlertDueTag+i];
        view.alpha = 0;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航控制器的代理为self
    self.navigationController.delegate = self;
    
    [KNotificationCenter addObserver:self selector:@selector(refreshGoodsData) name:LOGIN_SUCCESS object:nil];
    [KNotificationCenter addObserver:self selector:@selector(clearGoodsData) name:LOGIN_LOGOUT object:nil];
    [KNotificationCenter addObserver:self selector:@selector(popToHomePushRenZ) name:@"popToHomePushRenZ" object:nil];

    _isSegmentedLeft = YES;
    _isChufaFlag = YES;
    _chufaCode = @"";
    _shouhuoCode = @"";
    _pageNum = 1;
    
    [self setupBaseView];
    
    [self.view addSubview:self.confirmButton];
    [self.view addSubview:self.tableView];
    
    if (TOKEN_ISEMPTY) {
        self.foundImageView.hidden = NO;
    } else {
        [self supplyListRequest];
    }
}

- (void)setupBaseView {
//    WEAKSELF
    
    self.view.backgroundColor = [UIColor cy_colorWithHexString:@"#F1F1F1"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 135)];
    imageView.image = image(@"gcy_img_supply_banner");
    [self.view addSubview:imageView];
    
    
    NSArray *titleArr = @[@"货源专区", @"常跑专区"];
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.indicatorStyle = SGIndicatorStyleCover;
    configure.indicatorColor = [UIColor whiteColor];
    configure.titleColor = [UIColor whiteColor];
    configure.indicatorHeight = 40;
    configure.indicatorAdditionalWidth = 120;
    configure.titleSelectedColor = Dark_TextColor;
    configure.bottomSeparatorColor = [UIColor clearColor];
    /// pageTitleView
    SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake((ScreenW-270)/2, imageView.cy_bottom-30, 270, 30) delegate:self titleNames:titleArr configure:configure];
    pageTitleView.layer.cornerRadius = 2;
    pageTitleView.layer.masksToBounds = YES;
    pageTitleView.backgroundColor = RGBA(0, 0, 0, 0.6f);
    [self.view addSubview:pageTitleView];
    
//    SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, 270, 30) delegate:self titleNames:titleArr configure:configure];
//
//    [pageTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(imageView.mas_bottom);
//        make.centerX.equalTo(weakSelf.view);
//        make.size.mas_equalTo(CGSizeMake(260, 35));
//    }];
    
    UIButton *spanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    spanBtn.tag = 11;
    spanBtn.frame = CGRectMake(25, 40, 35, 35);
    cut(spanBtn, spanBtn.cy_width/2);
    spanBtn.backgroundColor = RGBA(0, 0, 0, 0.5);
    [spanBtn setImage:image(@"gcy_icon_hy_span") forState:UIControlStateNormal];
    [spanBtn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:spanBtn];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.tag = 10;
    searchBtn.frame = CGRectMake(ScreenW-60, 40, 35, 35);
    cut(searchBtn, searchBtn.cy_width/2);
    searchBtn.backgroundColor = RGBA(0, 0, 0, 0.5);
    [searchBtn setImage:image(@"gcy_icon_supply_search") forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchBtn];
    
    NSArray *btnTitles = @[@"出发地",@"目的地"];
    
    UIView *placeView = [[UIView alloc] initWithFrame:CGRectMake(0, imageView.cy_bottom, ScreenW, 45)];
    placeView.tag = 1000;
    [self.view addSubview:placeView];
    
    for (int i=0; i<2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(ScreenW/2*i, 0, ScreenW/2, 45);
        btn.tag = 100 + i;
        [btn setTitle:btnTitles[i] forState:UIControlStateNormal];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:Dark_TextColor forState:UIControlStateNormal];
        btn.titleLabel.font = font(15);
        btn.titleLabel.numberOfLines = 0;
        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [btn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:image(@"gcy_icon_supply_sTriangle") forState:UIControlStateNormal];
        [placeView addSubview:btn];
        
        [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10];
    }
    
    self.tableView.backgroundColor = [UIColor cy_colorWithHexString:@"#F1F1F1"];
    
    [self.view addSubview:self.barrierView];
    
    UIView *pickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH-TabBarHeight-180, ScreenW, 180)];
    pickerBgView.tag = 500;
    pickerBgView.backgroundColor = BackgroundColor;
    pickerBgView.hidden = YES;
    [self.view addSubview:pickerBgView];
    
    NSArray *pickerTitles = @[@"取消" ,@"确定"];
    for (int i=0; i<2; i++) {
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.tag = 200 + i;
        [cancelBtn setTitle:pickerTitles[i] forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = font(15);
        [cancelBtn setTitleColor:Dark_TextColor forState:UIControlStateNormal];
        if (i==0) {
            cancelBtn.frame = CGRectMake(10, 0, 60,40);
        }else {
            cancelBtn.frame = CGRectMake(ScreenW-80, 0, 60,40);
        }
        [cancelBtn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        [pickerBgView addSubview:cancelBtn];
    }
    
    [pickerBgView addSubview:self.pickerView];
}


#pragma mark - UISegmentedControl切换事件
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
//    NSLog(@"2222222222-%ld",selectedIndex);
    
    static BOOL isStartLoadLeftArea = NO;
    
    UIView *placeView = [self.view viewWithTag:1000];
    _pageNum = 1;
    if (selectedIndex == 0) {
        //货源专区
        _isSegmentedLeft = YES;
        placeView.hidden = NO;
        self.confirmButton.hidden = YES;
        self.tableView.frame = CGRectMake(0, 190, ScreenW, ScreenH-TabBarHeight-190);

        if (TOKEN_ISEMPTY) {
            
        } else {
            if (isStartLoadLeftArea) {
                [self supplyListRequest];
            } else {
                isStartLoadLeftArea = YES;
            }
        }
        
    } else {
        _isSegmentedLeft = NO;
        placeView.hidden = YES;
        self.confirmButton.hidden = NO;
        self.tableView.frame = CGRectMake(0, 135, ScreenW, ScreenH-TabBarHeight-135-65);
        
        if (TOKEN_ISEMPTY) {} else{
            [self oftenRunInfoRequest];
        }
    }
    _searchDic = nil;
//    [self.tableView reloadData];
}

//登录成功后回调
- (void)refreshGoodsData {
    [self supplyListRequest];
}

//退出登录后清空数据
- (void)clearGoodsData {
    
    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
    self.foundImageView.hidden = NO;
    
    _pageNum = 1;
}

#pragma mark - 货源列表请求
- (void)supplyListRequest {
    WEAKSELF
    
    if (TOKEN_ISEMPTY) {
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.foundImageView.hidden = NO;
        return;
    }
    
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params;
    
    if ([DCSpeedy isBlankDictionary:_searchDic] == NO) {
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"shipperSubdivisionCode" : _searchDic[@"shipperSubdivisionCode"],//装货地点国家行政区划代码
            @"consigneeSubdivisionCode" : _searchDic[@"consigneeSubdivisionCode"], //收货
            @"coalType" : _searchDic[@"coalType"],
            @"mainOrderNumber" : _searchDic[@"mainOrderNumber"],
            @"isFromOrderHall": @"1",//访问货源大厅必传1
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
    } else {
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"shipperSubdivisionCode" : _chufaCode,//装货地点国家行政区划代码
            @"consigneeSubdivisionCode" : _shouhuoCode, //收货
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
    }
    
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], ORDER_ORDERHALLLIST) withParameter:params withVC:self netName:@"更多货源列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.dataArray addObjectsFromArray:[MainActivityModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            
        } else {
            
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
                
        [XSInfoView showInfo:msg onView:weakSelf.view];
    }];
}

#pragma mark - 常跑信息列表请求
- (void)oftenRunInfoRequest {
    WEAKSELF
    
    if (TOKEN_ISEMPTY) {
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.foundImageView.hidden = NO;
        return;
    }
    
    if (_pageNum == 1) {
        [self.oftenRunArray removeAllObjects];
    }
    
    NSDictionary *parmas = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": @10,
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], COMMON_ROUTEINFO) withParameter:parmas withVC:self netName:@"常跑信息列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.oftenRunArray addObjectsFromArray:[OftenRunModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            
        }else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        [XSInfoView showInfo:msg onView:weakSelf.view];
    }];
}

#pragma mark - CYChooseRegionDelegate
- (void)getAllInfoProvinceCode:(NSString *)provinceCode provinceName:(NSString *)provinceName cityCode:(NSString *)cityCode cityName:(NSString *)cityName countyCode:(NSString *)countyCode countyName:(NSString *)countyName {
    
    _pageNum = 1;
    
    NSString *address = CJStringWithFormat(@"%@%@%@",provinceName,cityName,countyName);
    
    if(_isChufaFlag == YES) {
        if ([address isEqualToString:@"重置"]) {
            address = @"出发地";
        }
        
        if ([DCSpeedy isBlankString:countyCode] == NO) {
            _chufaCode = countyCode;
        } else if ([DCSpeedy isBlankString:cityCode] == NO) {
            _chufaCode = cityCode;
        }  else {
            _chufaCode = provinceCode;
        }
        _chufaAddress = address;
        
        UIButton *button = [self.view viewWithTag:100];
        [button setTitle:address forState:UIControlStateNormal];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10];
        
    } else {
        
        if ([address isEqualToString:@"重置"]) {
            address = @"目的地";
        }
        
        if ([DCSpeedy isBlankString:countyCode] == NO) {
            _shouhuoCode = countyCode;
        } else if ([DCSpeedy isBlankString:cityCode] == NO) {
            _shouhuoCode = cityCode;
        } else {
            _shouhuoCode = provinceCode;
        }
        _shouhuoAddress = address;
        
        UIButton *button = [self.view viewWithTag:101];
        [button setTitle:address forState:UIControlStateNormal];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10];
    }
    
    [self supplyListRequest];
}

- (void)btnTypeClick:(UIButton *)sender {
    
    if ([DCSpeedy judgeUserIsLogin] == NO) return;
    
    UIView *pickerBgView = [self.view viewWithTag:500];
    
    if (sender.tag == 10) {
        //搜索
//        if (_isSegmentedLeft == YES) {
//
//        } else {
//            [XSInfoView showInfo:@"请切换至货源专区" onView:self.view];
//        }
        if (TOKEN_ISEMPTY) {
            [XSInfoView showInfo:@"请先登录" onView:self.view];
            return;
        }
        SearchHuoYuanViewController *shyvc = [[SearchHuoYuanViewController alloc] init];
        shyvc.delegate = self;
        [self.navigationController pushViewController:shyvc animated:YES];
        
    } else if (sender.tag == 11) {
        //扫码装卸货
        //设置扫码区域参数
        LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
        //扫码框中心位置与View中心位置上移偏移像素(一般扫码框在视图中心位置上方一点)
        style.centerUpOffset = 44;
        //扫码框周围4个角的类型设置为在框的上面,可自行修改查看效果
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_On;
        //扫码框周围4个角绘制线段宽度
        style.photoframeLineW = 6;
        //扫码框周围4个角水平长度
        style.photoframeAngleW = 24;
        //扫码框周围4个角垂直高度
        style.photoframeAngleH = 24;
        //动画类型：网格形式，模仿支付宝
        style.anmiationStyle = LBXScanViewAnimationStyle_NetGrid;
        //动画图片:网格图片
        style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_part_net"];;
        //扫码框周围4个角的颜色
        style.colorAngle = [UIColor colorWithRed:65./255. green:174./255. blue:57./255. alpha:1.0];
        //是否显示扫码框
        style.isNeedShowRetangle = YES;
        //扫码框颜色
        style.colorRetangleLine = [UIColor colorWithRed:247/255. green:202./255. blue:15./255. alpha:1.0];
        //非扫码框区域颜色(扫码框周围颜色，一般颜色略暗)
        //必须通过[UIColor colorWithRed: green: blue: alpha:]来创建，内部需要解析成RGBA
        style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        ScanViewController *vc = [[ScanViewController alloc] init];
        vc.libraryType = SLT_Native;
        vc.style = style;
        vc.pageFlag = 1;
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if (sender.tag == 100) {
        //选择出发地
        if ([DCSpeedy judgeUserIsLogin] == NO) return;
        
        _isChufaFlag = YES;
            
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 0;
        [CYCityChooseView shared].delegate = self;
        
    } else if (sender.tag == 101) {
        //选择目的地
        if ([DCSpeedy judgeUserIsLogin] == NO) return;
        
        _isChufaFlag = NO;
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 0;
        [CYCityChooseView shared].delegate = self;
        
    } else {
        _barrierView.hidden = YES;
        pickerBgView.hidden = YES;
        
        if (sender.tag == 200) {
            //picker取消
            
        }else if (sender.tag == 201) {
            //picker确定
            UIButton *chufaBtn = [self.view viewWithTag:100];
            UIButton *shouhuoBtn = [self.view viewWithTag:101];
            
            _isChufaFlag == YES ? [chufaBtn setTitle:_chufaAddress forState:UIControlStateNormal] : [shouhuoBtn setTitle:_shouhuoAddress forState:UIControlStateNormal];
            
            _pageNum = 1;
            [self supplyListRequest];
        }
    }
    
}

#pragma mark - HuoYuanSearchConditionDelegate
- (void)GetSearchConditionResult:(NSDictionary *)dictionary {
    
    _searchDic = dictionary;
    [self supplyListRequest];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isSegmentedLeft == YES) {
        return self.dataArray.count;
    } else {
        return self.oftenRunArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_isSegmentedLeft == YES) {
        tableView.rowHeight = 215;
        SupplyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.dataArray.count!=0) {
            cell.supplyModel = self.dataArray[indexPath.row];
        }
        return cell;
    } else {
        tableView.rowHeight = 65;
        OftenRunCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.oftenRunArray.count!=0) {
            cell.model = self.oftenRunArray[indexPath.row];
        }
        return cell;
    }
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (_isSegmentedLeft) {
//        return 160;
//    } else {
//        return 65;
//    }
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([DCSpeedy judgeUserIsLogin] == NO) return;
    
    if (_isSegmentedLeft) {
        MainActivityModel *model = self.dataArray[indexPath.row];
        HuoYuanDetailViewController *hydVC = [[HuoYuanDetailViewController alloc] init];
        hydVC.orderCode = model.code;
        [self.navigationController pushViewController:hydVC animated:YES];
        
    } else {
        
        OftenRunModel *model = self.oftenRunArray[indexPath.row];
        HuoYuanListViewController *hylvc = [[HuoYuanListViewController alloc] init];
        hylvc.shipperSubdivisionCode = model.consignorCountyCode;
        hylvc.consigneeSubdivisionCode = model.consigneeCountyCode;
        [self.navigationController pushViewController:hylvc animated:YES];
    }
}

- (void)confirmButtonClick {
    
    if ([DCSpeedy judgeUserIsLogin] == NO) return;
    
    AddOftenRunRouteViewController *aorrvc = [[AddOftenRunRouteViewController alloc] init];
    [self.navigationController pushViewController:aorrvc animated:YES];
    aorrvc.addOftenRunRouteBlock = ^(BOOL isSuccess) {
        if (isSuccess) {
            if (_isSegmentedLeft == NO) {
                [self oftenRunInfoRequest];
            }
        }
    };
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    if (_isSegmentedLeft) {
        _searchDic = nil;
        [self supplyListRequest];
    } else {
        [self oftenRunInfoRequest];
    }
    
}
- (void)loadMoreData{
    _pageNum ++;
    if (_isSegmentedLeft) {
        [self supplyListRequest];
    } else {
        [self oftenRunInfoRequest];
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        
        _tableView.frame = CGRectMake(0, 190, ScreenW, ScreenH-TabBarHeight-190);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"SupplyCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [_tableView registerNib:[UINib nibWithNibName:@"OftenRunCell" bundle:nil] forCellReuseIdentifier:@"cellTwo"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)oftenRunArray {
    if (!_oftenRunArray) {
        _oftenRunArray = [[NSMutableArray alloc] init];
    }
    return _oftenRunArray;
}

- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = [self.tableView convertPoint:self.tableView.center fromView:self.tableView.superview];
        [self.tableView addSubview:_foundImageView];
    }
    return _foundImageView;
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"新增常跑路线" forState:UIControlStateNormal];
        [_confirmButton setBackgroundColor:Theme_ColorValue];
        _confirmButton.hidden = YES;
        cut(_confirmButton, BTN_BORDER_RADIUS);
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmButton.frame = CGRectMake(20, ScreenH - TabBarHeight - 55, ScreenW - 40, 45);
    }
    return _confirmButton;
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)popToHomePushRenZ{
    /**
     authStatus
     0未审核/1审核中/2审核未通过/3审核通过
     */
    if ([[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue] == 0) {
        
        DriverAuthVC *davc = [[DriverAuthVC alloc] init];
        [self.navigationController pushViewController:davc animated:YES];
        
    } else {
        
        DriverAuthVC *davc = [[DriverAuthVC alloc] init];
        davc.isNotEdit = YES;
        [self.navigationController pushViewController:davc animated:YES];
    }
}
- (void)dealloc {
    [KNotificationCenter removeObserver:self name:LOGIN_SUCCESS object:nil];
    [KNotificationCenter removeObserver:self name:LOGIN_LOGOUT object:nil];
    [KNotificationCenter removeObserver:self name:@"popToHomePushRenZ" object:nil];
    
}

@end
